package leecode

import "math"

func findMaxAverage(nums []int, k int) (result float64) {
	result = math.MinInt64
	//初始化窗口
	sum := 0
	for i := 0; i < k; i++ {
		sum += nums[i]
	}
	avg := float64(sum) / float64(k)
	if avg > result {
		result = avg
	}
	for right := 1; right+k <= len(nums); right++ {
		sum -= nums[right-1]
		sum += nums[right+k-1]
		avg = float64(sum) / float64(k)
		if avg > result {
			result = avg
		}
	}
	return
}
